class_name StateMachine
extends Node

##默认状态
@export var current_state: StateBase
##状态机所属CharacterBody2D节点
@export var state_machine_owner: CharacterBody2D

var state_time: float

func _ready() -> void:
	#获取状态机下的各个状态
	for child in get_children():
		#防止获取非状态节点`
		if child is StateBase:
			child.state_machine = self
	#等待父节点完成初始化
	await state_machine_owner.ready
	current_state.enter()
	state_time = 0

func _process(delta: float) -> void:
	current_state.process_update(delta)

func _physics_process(delta: float) -> void:
	current_state.physics_process_update(delta)
	state_time += delta

#切换状态
func change_state(target_state_name: String) -> void:
	var target_state = get_node_or_null(target_state_name)
	
	if target_state == null:
		print("状态传输出错")
		return
	current_state.exit()
	current_state = target_state
	current_state.enter()
	
	state_time = 0
